<?php

/**
 * MLEPP - ManiaLive Extending Plugin Pack
 *
 * -- MLEPP Plugin --
 * @name HeadsUp
 * @date 07-06-2011
 * @version r934
 * @website mlepp.trackmania.nl
 * @package MLEPP
 * @author The MLEPP team
 * @copyright 2011
 *
 * ---------------------------------------------------------------------
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------
 * You are allowed to change things or use this in other projects, as
 * long as you leave the information at the top (name, date, version,
 * website, package, author, copyright) and publish the code under
 * the GNU General Public License version 3.
 * ---------------------------------------------------------------------
 */

namespace ManiaLivePlugins\MLEPP\HeadsUp;

use ManiaLive\Utilities\Console;
use ManiaLive\Data\Storage;
use ManiaLive\DedicatedApi\Connection;
use ManiaLive\Config\Loader;
use ManiaLive\Event\Dispatcher;
use ManiaLivePlugins\MLEPP\HeadsUp\Gui\Windows\SimpleWindow;
use ManiaLivePlugins\MLEPP\HeadsUp\Gui\Windows\HeadsUpWidget;
use ManiaLivePlugins\MLEPP\Core\Core;
use ManiaLivePlugins\MLEPP\Core\Mlepp;

class HeadsUp extends \ManiaLive\PluginHandler\Plugin {
	private $mlepp = NULL;
	public static $widgetPosition = "30,-47";
	public static $widgetWidth = "22";
	public static $widgetText = "\$s$00dMLEPP Server Control|  \$z\$sClick for more info";
	public static $website = "http://mlepp.trackmania.nl";

    /**
     * onInit()
     * Function called on initialisation of ManiaLive.
     *
     * @return void
     */

	function onInit() {
		// this needs to be set in the init section
		$this->setVersion(934);
		$this->setPublicMethod('getVersion');
		//ML Repository
		if (Core::RepositoryId != null) {
			$this->setRepositoryId(Core::RepositoryId);
			$this->setRepositoryVersion(Core::RepositoryVersion);
		}

	}

    /**
     * onLoad()
     * Function called on loading of ManiaLive.
     *
     * @return void
     */

	function onLoad() {
		$this->enableDedicatedEvents();
		$this->mlepp = Mlepp::getInstance();


		Console::println('[' . date('H:i:s') . '] [MLEPP] Plugin: HeadsUp r' . $this->getVersion() );
	}

    function onUnload() {
			parent::onUnload();
	}
	/**
	 * onReady()
     * Function called when ManiaLive is ready loading.
	 *
	 * @return void
	 */

	function onReady() {
		$text = self::$widgetText;
		foreach ($this->storage->players as $login => $player) {
			$this->showWidget($login, $text);
		}
		foreach ($this->storage->spectators as $login => $player) {
			$this->showWidget($login, $text);
		}
	}



    /**
     * onBeginChallenge()
     * Function called on begin of challenge.
     *
     * @param mixed $challenge
     * @param mixed $warmUp
     * @param mixed $matchContinuation
     * @return void
     */

	function onBeginChallenge($challenge, $warmUp, $matchContinuation) {
		$text = self::$widgetText;
		foreach ($this->storage->players as $login => $player) {
			$this->showWidget($login, $text);
		}
		foreach ($this->storage->spectators as $login => $player) {
			$this->showWidget($login, $text);
		}
	}

	/**
	 * showWidget()
     * Helper function, shows the widget.
	 *
	 * @param mixed $login
	 * @param mixed $text
	 * @return void
	 */

	function showWidget($login, $text) {

		$width = self::$widgetWidth;
		$window = HeadsUpWidget::Create($login);
		$window->setSize($width, 9);
		$window->setText($text);
		$pos = explode(",", self::$widgetPosition);
		$window->setPosition($pos[0], $pos[1]);
		$window->show();
	}

	/**
	 * onPlayerConnect()
     * Function called when a player connects.
	 *
	 * @param mixed $login
	 * @param mixed $isSpectator
	 * @return void
	 */

	function onPlayerConnect($login, $isSpec) {
		$text = self::$widgetText;
		$this->showWidget($login, $text);
	}
}
?>